package com.huifaapp.mall.jpush;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import com.blankj.utilcode.util.LogUtils;
import com.huifaapp.mall.R;

import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;


/**
 * 自定义接收器
 * 
 * 如果不定义这个 Receiver，则： 1) 默认用户会打开主界面 2) 接收不到自定义消息
 */
public class JpushReceiver extends BroadcastReceiver {
	private static final String TAG = "JPush";
	private static final int ID_NOTICE = 0x001;

	@Override
	public void onReceive(Context context, Intent intent) {
		Bundle bundle = intent.getExtras();
		// LogUtils.e(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ",
		// extras: " + printBundle(bundle));
		
		if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
			String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
			LogUtils.e(TAG, "[MyReceiver] 接收Registration Id : " + regId);
			// send the Registration Id to your server...

		} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
			LogUtils.e(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
			 processCustomMessage(context, bundle);

		} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
			LogUtils.e(TAG, "[MyReceiver] 接收到推送下来的通知>>"+bundle.getString(JPushInterface.EXTRA_EXTRA));
			int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
			LogUtils.e(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);

		} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
			LogUtils.e(TAG, "[MyReceiver] 用户点击打开了通知");
			//
			String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
			try {
				JSONObject jo = new JSONObject(extra);
//				String joDdid = jo.optString("orderid");
//				Intent notificationIntent = new Intent(context, OrderDetaActivity.class);
//				notificationIntent.putExtra(OrderDetaActivity.KEY_ORDER_ID,joDdid);
//				notificationIntent.putExtra(OrderDetaActivity.KEY_IS_BACK_ORDER_LIST, true);
//				notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
//				context.startActivity(notificationIntent);
			}catch (Exception e){

			}




//			// 打开自定义的Activity
//			Intent i = new Intent(context, MainActivity.class);
//			i.putExtras(bundle);
//			// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//			context.startActivity(i);

		} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
			LogUtils.e(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
			// 在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity，
			// 打开一个网页等..

		} else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
			boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
			LogUtils.e(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected);
		} else {
			LogUtils.e(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
		}
	}

	private void processCustomMessage(Context context, Bundle bundle) {
		// String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
		String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
		LogUtils.e(">>>>>>>>>>.", extras);
//		 PushMessageBean pushMessageBean = FastJsonUtils.getPerson(extras,
//		 PushMessageBean.class);
		/*if(extras!=null&&!TextUtils.isEmpty(extras)) {
			showNotification(context, extras);
		}else {*/
			showNotification(context, "您有一条未读消息");
		//}


	}

	private void showNotification(Context context, String str) {
		// String uid = HXCoastApplication.getInstance().getUid();
//		switch (pushMessageBean.getType()) {
//		case PushMessageBean.TYPE_KICK:// 强制踢出登录
//			Intent intent = new Intent(context, SettingActivity.class);
////		intent.putExtra(PersonalInformationActivity.KEY_SHOW_FRAGMENTL_NUM,1);
//			notice(context, str, intent);
//			break;

//		default:
//			break;
//		}
	}

	private void notice(Context context, String strr, Intent intent) {

		builderNotification(context, strr, intent);
	}

	/**
	 * 
	 * @param context
	 * @param contentTitle
	 *            通知栏标题
	 * @param contentText
	 *            通知栏内容
	 * @param smallTitle
	 *            通知栏小标题
	 * @param intent
	 *            点击跳转的Intent
	 */
	private void builderNotificationWithIcon(Context context, String sstr, Intent intent,
                                             Bitmap bitmap) {

		PendingIntent contentIntent = PendingIntent.getActivity(context, ID_NOTICE, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);
		NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification.Builder builder = new Notification.Builder(context);
		builder.setSmallIcon(R.drawable.ic_launcher).setLargeIcon(bitmap)
				.setContentTitle(sstr)
				.setWhen(System.currentTimeMillis()).setTicker(sstr)
				.setAutoCancel(true).setContentIntent(contentIntent);
		Notification notification = builder.getNotification();
		notification.defaults = Notification.DEFAULT_SOUND;

		nm.notify(ID_NOTICE, notification);
	}

	private void builderNotification(Context context, String strrr, Intent intent) {

		Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
		builderNotificationWithIcon(context, strrr, intent, bitmap);
	}

}
